package com.qcxk.yuqian.system.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.UUID;

import com.qcxk.yuqian.system.entity.WordXmlBean;
import com.qcxk.yuqian.system.utils.WordXmlUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/word")
public class WordController {

    @Autowired
    private HttpServletRequest request;

    /**
     * @param wordType
     * @return
     */
    @RequestMapping("/exportWord")
    @ResponseBody
    public Map<String, Object> exportWord(String wordType) {

        //获取WordXml模板、选项、word保存路径
        WordXmlBean wordXmlBean = WordXmlUtil.getXmlAndOptions(wordType);

        Map<String, Object> result = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = new File(wordXmlBean.getSrcWordPath()).getName();
        fileName = fileName.substring(0, fileName.lastIndexOf("."));
        fileName += "_" + sdf.format(new Date()) + "_" + UUID.randomUUID().toString().replaceAll("-", "").substring(0, 6);

        //变量
        Map<String, Object> dynamicVariables = new HashMap<String, Object>();
//		//选中的序号
//		Map<String, List<Integer>> checkedOptions = new HashMap<String, List<Integer>>();

        Map<String, String[]> parameters = request.getParameterMap();
        Iterator<Entry<String, String[]>> it = parameters.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, String[]> next = it.next();
            String key = next.getKey();
            String[] values = next.getValue();
            dynamicVariables.put(key, StringUtils.join(values, ","));
            //登记日期
            if ("2".equals(wordType) && "registerDate".equals(key)) {
                for (String value : values) {
                    if (value.split("-").length >= 3) {
                        dynamicVariables.put("y", value.split("-")[0]);
                        dynamicVariables.put("m", value.split("-")[1]);
                        dynamicVariables.put("d", value.split("-")[2]);
                    }
                }
            }
            //填表日期
            if (("4".equals(wordType) || "5".equals(wordType) || "6".equals(wordType)) && "filledAt".equals(key)) {
                for (String value : values) {
                    if (value.split("-").length >= 3) {
                        dynamicVariables.put("y", value.split("-")[0]);
                        dynamicVariables.put("m", value.split("-")[1]);
                        dynamicVariables.put("d", value.split("-")[2]);
                    }
                }
            }
        }

        WordXmlUtil.generateWord(wordXmlBean.getSrcWordPath(), wordXmlBean.getWordPath(), fileName, dynamicVariables, wordXmlBean.getAllOptions(), wordType);
//		WordXmlUtil.generateWord(wordXmlBean.getSrcWordPath(), wordXmlBean.getWordPath() + "/" + null, fileName, dynamicVariables, wordXmlBean.getAllOptions(), checkedOptions, wordType);

        result.put("rs", 1);
        result.put("wordPath", wordXmlBean.getWordUrl() + "/" + fileName + ".docx");
        return result;
    }

}
